package com.wyth.lottery.utils;

import android.app.Activity;
import android.content.Intent;
import android.hardware.usb.UsbDevice;

import com.wyth.lottery.activity.PrizeActivity;

/**
 * Created by Administrator on 2017/9/25
 */

public class VbarUtils {
    public static boolean state = false;
    public static boolean decodestate = false;
    public static Vbar b = new Vbar();
    public static UsbPrinterUtil mUtil;
    public static Activity acy;
    public static Thread thread;

    public static void initVbar(final Activity activity, final String start_state) {
        acy = activity;
        decodestate = false;

        try {
            thread = Thread.currentThread();
            thread.sleep(10);//暂10毫秒后程序继续执行
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        b.vbarClose();
        state = b.vbarOpen();
        if (state) {
            MyToastUtils.showToast(activity, "连接成功!");
            decodestate = true;
        } else {
            MyToastUtils.showToast(activity, "连接失败!");
            decodestate = false;
        }

        Thread t = new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                while (decodestate) {

                    final String str = b.vbarScan();

                    acy.runOnUiThread(new Runnable() {
                        public void run() {

                            try {
                                thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            if (str != null) {
                                b.vbarBeep(1);
                                if (start_state.equals("1") | start_state.equals("2")) {
                                    acy.startActivity(new Intent(activity, PrizeActivity.class));
                                    decodestate = !decodestate;
                                } else {
                                    acy.startActivity(new Intent(activity, PrizeActivity.class));
                                    openSub();
                                }

                                return;

                            }

                        }
                    });
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();

    }

    protected static void openSub() {
        mUtil = new UsbPrinterUtil(acy);

        UsbDevice dev = mUtil.getUsbPrinterList().get(0);

        UsbPrinter prt = null;
        mUtil.bitmaptest(acy);


        try {

            prt = new UsbPrinter(acy, dev);
            prt.cutPaper(acy);

            prt.cutPaper();
        } catch (Exception e) {
            messageBox(acy);
        } finally {
            if (prt != null) {
                prt.close();
            }
        }
    }

    protected static void messageBox(Activity acy) {
        MyToastUtils.showToast(acy, "出错啦..");
    }
}
